<?php

namespace App\Admin\Controllers\education;

use App\Admin\Renderable\AdminGroupTable;
use App\Admin\Renderable\SchoolTable;
use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\AdminSchool;
use App\Models\AdminEducation;
use App\Models\AdminUserGroup;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Card;

class ListController extends AdminController
{
    protected $title = "助学";

    protected $translation = 'admin-education';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(AdminEducation::with(['user', 'admin']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('cover_path') -> image('','100');
            $grid->column('info','基本信息') -> display(
                function(){
                    return <<<HTML
                       <div style="line-height: 1.8; font-size: 14px; text-align: left;">
                            <div><strong>标题：</strong> {$this->title}</div>
                            <div><strong>发布人：</strong> {$this->admin->name} - {$this->admin->phone}</div>
                            <div><strong>负责人：</strong> {$this->user->name} - {$this->user->phone}</div>
                        </div>
                    HTML;
                }
            );
            $grid->column('other','项目信息') -> display(
                function(){
                    return <<<HTML
                       <div style="line-height: 1.8; font-size: 14px; text-align: left;">
                            <div><strong>已筹：</strong> {$this->target_amount}</div>
                            <div><strong>目标：</strong> {$this->target_amount}</div>
                            <div><strong>累计：</strong> {$this->target_amount}</div>
                            <div><strong>划拨：</strong> {$this->target_amount}</div>
                        </div>
                    HTML;
                }
            );
            $grid->column('content') -> display('查看') -> modal(
                function ($modal) {
                    $modal->title($this->title);
                    $content = <<<HTML
                        <div class="rich-content">
                            {$this->content}
                        </div>
                        <style>
                            .rich-content {
                                width: 100%;
                                word-break: break-word;
                            }
                            .rich-content img {
                                max-width: 100%;
                                height: auto;
                                display: block;
                            }
                        </style>
                    HTML;
                    $card = new Card(null, $content);
                    return "<div style='padding:10px 10px 0'>$card</div>";
                }
            );
            $grid->column('online_status') -> switch();
            $grid->showEditButton();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableQuickEdit();
            });
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('online_status', '上线状态', ['已下线', '已上线']);
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdminEducation(), function (Form $form) {
            $form->title('编辑助学');
            $form -> disableDeleteButton();
            $form -> disableViewButton();
            $form -> disableResetButton();
            $form
                ->tab('基本信息', fn($form) => $this -> firstFrom($form))
                ->tab('选填信息', fn($form) => $this -> secondFrom($form))
                ->tab('助学详情', fn($form) => $this -> thirdFrom($form));

            $form -> saving(function (Form $form) {
                $data = $form -> input();

                $model = AdminEducation::find($form -> model() -> id);

                $res = $model->update($data);

                if(!$res) {
                    return $form -> response() -> error('操作失败');
                }

                return $form -> response() ->success('操作成功') -> redirect('/education/list');
            });
        });
    }

    public function firstFrom($form)
    {
        $form->text('title')
            ->required();

        $form->textarea('description')
            ->required();

        $form->radio('educational_type')
            ->options(function () {
                return trans('admin-education.options.educational_type');
            })
            ->default(0)
            ->required();

        $map_data = [
            'name'      => 'address',
            'address'   => $form->model()->address,
            'longitude' => $form->model()->longitude,
            'latitude'  => $form->model()->latitude
        ];

        $form->html(view('admin.components.map-dialog', $map_data)->render(),'助学地址')
            ->required();

        $form->number('target_amount')
            ->required();

        $form->selectTable('user_id')
            ->from(UserTable::make(['id' => $form->getKey()]))
            ->model(User::class, 'id', 'name')
            ->required();

        $form->selectTable('group_id')
            ->from(AdminGroupTable::make(['id' => $form->getKey()]))
            ->model(AdminUserGroup::class, 'id', 'name')
            ->required();

        $form->image('cover_path')
            ->url('/upload')
            ->uniqueName()
            ->autoUpload()
            ->maxSize(5120)
            ->required();
    }

    public function secondFrom($form)
    {
        $student_info = [
            'title' => '学生',
            'left_name' => 'student_name',
            'left_value' => $form->model()->student_name,
            'right_name' => 'student_phone',
            'right_value' => $form->model()->student_phone
        ];
        $form->html(view('admin.components.step-column', $student_info)->render(), '学生信息');
        $head_teacher_info = [
            'title' => '班主任',
            'left_name' => 'head_teacher',
            'left_value' => $form->model()->head_teacher,
            'right_name' => 'head_teacher_phone',
            'right_value' => $form->model()->head_teacher_phone,
        ];
        $form->html(view('admin.components.step-column', $head_teacher_info)->render(), '班主任信息');
        $guardian_info = [
            'title' => '监护人',
            'left_name' => 'guardian_name',
            'left_value' => $form->model()->guardian_name,
            'right_name' => 'guardian_phone',
            'right_value' => $form->model()->guardian_phone,
        ];
        $form->html(view('admin.components.step-column', $guardian_info)->render(), '监护人信息');
        $form->radio('student_sex')->options(trans('global.options.sex'));
        $form->text('student_address');
        $form->text('class_name');
        $form->date('student_birthday');
        $form->selectTable('school_id')
            ->from(SchoolTable::make())
            ->model(AdminSchool::class, 'id', 'school_name');
        $form->date('enrollment_year');
        $form->text('source');
        $form->text('guardian_relation');
        $form->text('payment_info');
    }

    public function thirdFrom($form)
    {
        $form->editor('content')->required();
    }
}
